#include "game.h"
#include "lcd12864.c"
/**
 * 在屏幕指定位置显示一个数字
 * @param num 0~9 任意数字
 */
void display_num(
    unsigned char page,
    unsigned char col,
    unsigned char num)
{
    lcd_display_bytes_in_page(page, col, &NUM_CHARS[num * 4], 4);
}
void display_speed_select_screen()
{
    // 显示上下两个黑条
    lcd_display_same_bytes_in_page(0, 0, 0xFF, 128);
    lcd_display_same_bytes_in_page(7, 0, 0xFF, 128);

    // page 3 ，52列，写SPEED
    lcd_display_bytes_in_page(3, 52, SPEED_CHARS, sizeof(SPEED_CHARS));

    // page 4, 写箭头
    lcd_display_bytes_in_page(4, 57, LEFT_ARROW, sizeof(LEFT_ARROW));
    lcd_display_bytes_in_page(4, 67, RIGHT_ARROW, sizeof(RIGHT_ARROW));
}

void display_speed_select_num(unsigned char num)
{
    display_num(4, 62, num);
}

void display_init()
{
    lcd_init();
    lcd_clean_screen();
}

void fill_display_block_arr(
    bit top_is_block,
    bit bottom_is_block,
    unsigned char display_byte[])
{
    unsigned char temp;
    temp = 0x00;
    if (top_is_block == 1)
    {
        temp |= 0x0F;
    }

    if (bottom_is_block == 1)
    {
        temp |= 0xF0;
    }
    display_byte[0] = temp;
    display_byte[1] = temp & 0xBB;
    display_byte[2] = temp & 0xDD;
    display_byte[3] = temp;
}

void display_next_shape(unsigned int shpae_data)
{
    // HHHH LLLL(page6) | xxxx xxxx(page7)
    // HHHH  16byte
    // LLLL
    // shape_data 中的两个bit 转换成为4byte，作为屏幕显示数据
    // 屏幕上显示 上下排列的两个方块

    // 15 14 13 12  11  10  9  8  page
    // 7   6  5  4   3   2  1  0  page+1
    unsigned char i;
    bit top;
    bit bottom;
    unsigned char display_byte[] = {0, 0, 0, 0};
    for (i = 0; i < 4; i++)
    {
        top = ((shpae_data >> (15 - i)) & 0x0001) == 1;
        bottom = ((shpae_data >> (11 - i)) & 0x0001) == 1;
        // top bottom 作为输入项，数组指针作为输入项
        // 根据top和bottom的值，填充数组
        fill_display_block_arr(top, bottom, display_byte);
        lcd_display_bytes_in_page(6, 8 + 4 * i, display_byte, 4);

        top = ((shpae_data >> (7 - i)) & 0x0001) == 1;
        bottom = ((shpae_data >> (3 - i)) & 0x0001) == 1;
        fill_display_block_arr(top, bottom, display_byte);
        lcd_display_bytes_in_page(7, 8 + 4 * i, display_byte, 4);
    }
}

void display_right_logo()
{
    unsigned char i;
    unsigned char word_byte_width = 16;
    // 写两个汉字
    for (i = 0; i < 2; i++)
    {
        // 0~15 第一页，col
        // 16~31 第二页, col
        lcd_display_bytes_in_page(1, 85 + word_byte_width * i, &RIGHT_WORDS_CN[word_byte_width * 2 * i], word_byte_width);
        lcd_display_bytes_in_page(2, 85 + word_byte_width * i, &RIGHT_WORDS_CN[word_byte_width * (2 * i + 1)], word_byte_width);
    }

    word_byte_width = 8;
    // 写6个英文字母， 8x16，占两页
    for (i = 0; i < 6; i++)
    {
        lcd_display_bytes_in_page(3, 77 + word_byte_width * i, &RIGHT_WORDS_EN[word_byte_width * 2 * i], word_byte_width);
        lcd_display_bytes_in_page(4, 77 + word_byte_width * i, &RIGHT_WORDS_EN[word_byte_width * (2 * i + 1)], word_byte_width);
    }
}

void display_pause()
{
    unsigned char i;
    unsigned char word_byte_width = 8;
    for (i = 0; i < 5; i++)
    {
        lcd_display_bytes_in_page(6, 81 + word_byte_width * i, &PAUSE_CHARS[word_byte_width * 2 * i], word_byte_width);
        lcd_display_bytes_in_page(7, 81 + word_byte_width * i, &PAUSE_CHARS[word_byte_width * (2 * i + 1)], word_byte_width);
    }
}